package com.aletto.general_store.ui.popup;

import android.content.Context;
import android.view.View;
import android.widget.RadioGroup;

import com.aletto.general_store.R;
import com.lxj.xpopup.core.AttachPopupView;

import androidx.annotation.NonNull;

public class GoodsStoreSwitchPopup extends AttachPopupView {

    private int searchType;

    private RadioGroup switchRadioGroup;

    public static int GOODS_TYPE = 1, STORE_TYPE = 2;

    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener;

    public GoodsStoreSwitchPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_switch_goods_sotre;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        switchRadioGroup = findViewById(R.id.switch_radio_group);

        if (searchType == GOODS_TYPE) {
            switchRadioGroup.check(R.id.switch_goods_radio);

        } else if (searchType == STORE_TYPE) {
            switchRadioGroup.check(R.id.switch_store_radio);
        }

        //switchRadioGroup.check(R.id.switch_goods_radio); // 默认商品

        switchRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            mOnCheckedChangeListener.onCheckedChanged(group, checkedId);

            dismiss();
        });
    }

    public GoodsStoreSwitchPopup setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
        // RadioButton checkRadioButton = group.findViewById(checkedId);
        //        mSwitchTextView.setText(checkRadioButton.getText());

        return this;
    }

    public void setSearchType(int searchType) {
        this.searchType = searchType;

        if (switchRadioGroup == null) return;

        if (searchType == GOODS_TYPE) {
            switchRadioGroup.check(R.id.switch_goods_radio);

        } else if (searchType == STORE_TYPE) {
            switchRadioGroup.check(R.id.switch_store_radio);
        }
    }

}
